﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;


namespace Common.Eventing.Serializers
{
    public class BinaryEventSerializer : IEventSerializer
    {
        public byte[] Serialize(object message)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream buffer = new MemoryStream();

            formatter.Serialize(buffer, message);

            return buffer.ToArray();
        }

        public object Deserialize(byte[] memento)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream buffer = new MemoryStream(memento);

            return formatter.Deserialize(buffer);
        }

        public TEvent Deserialize<TEvent>(byte[] memento)
        {
            return (TEvent)this.Deserialize(memento);
        }
    }
}
